package com.kaifamiao.demo.d0802;

import java.util.Arrays;

public class MyStack implements Stack{

    private char[] chs;

    private int size = 0;

    public MyStack(){
        chs = new char[10];
    }

    public MyStack(int size){
        if (size > 0) {
            chs = new char[size];
        } else{
            chs = new char[10];
        }
    }

    @Override
    public void push(char ch) {
        if (size == chs.length){
            grow(); // 扩容
        }
        // chs[size ++] = ch;
        chs[size] = ch;
        size ++;
    }

    private void grow() {
        chs = Arrays.copyOf(chs, chs.length + 10);
    }

    @Override
    public char pop() {
        char old = chs[size - 1];
        chs[size - 1] = '\u0000';
        size --;
        return old;
    }

    @Override
    public char top() {
        return 0;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean empty() {
        return size == 0;
    }
}
